package com.boyuan.huijiaoyu;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.example.contactfragment.ContactService;
import com.magicsoft.app.wcf.listener.GetMultilParamsListener;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class ZhuceYonghuActivity extends Activity implements OnClickListener{

	private EditText edit_user;
	private EditText edit_pwd;
	private EditText edit_mobile;
	private EditText edit_mail;
	private ContactService contactService;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhece_yonghu);
		initUI();
	}

	private void initUI() {
		edit_user = (EditText) findViewById(R.id.edit_user);
		edit_pwd = (EditText) findViewById(R.id.edit_pwd);
		edit_mobile = (EditText) findViewById(R.id.edit_mobile);
		edit_mail = (EditText) findViewById(R.id.edit_mail);
		findViewById(R.id.lay_back).setOnClickListener(this);
		findViewById(R.id.lay_save).setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.zhece_yonghu, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lay_back:
			finish();
			break;
		case R.id.lay_save:
			zhuce();
			break;
		default:
			break;
		}
	}

	private void zhuce() {
		if (contactService == null) {
			contactService = new ContactService(this);
		}
		
		String account = edit_user.getText().toString().trim();
		String password = edit_pwd.getText().toString().trim();
		String mima = getMD5(password);
		String mobile = edit_mobile.getText().toString().trim();
		String mail = edit_mail.getText().toString().trim();
		contactService.zhuce(account, mima, mobile, mail, new GetMultilParamsListener() {
			
			@Override
			public void onFinish(Object... params) {
				Toast.makeText(ZhuceYonghuActivity.this, "注册成功", Toast.LENGTH_LONG).show();
				finish();
			}
			
			@Override
			public void onFailed(String Message) {
				Toast.makeText(ZhuceYonghuActivity.this, Message, Toast.LENGTH_LONG).show();
			}
		});
	}
	
	public String getMD5(String info)
	{
		try
		{
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(info.getBytes("UTF-8"));
			byte[] encryption = md5.digest();

			StringBuffer strBuf = new StringBuffer();
			for (int i = 0; i < encryption.length; i++)
			{
				if (Integer.toHexString(0xff & encryption[i]).length() == 1)
				{
					strBuf.append("0").append(Integer.toHexString(0xff & encryption[i]));
				}
				else
				{
					strBuf.append(Integer.toHexString(0xff & encryption[i]));
				}
			}

			return strBuf.toString();
		}
		catch (NoSuchAlgorithmException e)
		{
			return "";
		}
		catch (UnsupportedEncodingException e)
		{
			return "";
		}
	}

}
